<?php

namespace common\models;

use common\helpers\commonApi;
use Yii;

/**
 * This is the model class for table "{{%order_goods}}".
 *
 * @property int $id
 * @property int|null $order_id 订单id
 * @property int|null $good_id 商品id
 * @property int|null $spec_id 规格id
 * @property string|null $spec_name 规格名称
 * @property string|null $good_name 商品名称
 * @property string|null $good_info 商品信息
 * @property string|null $spec_info 规格信息
 * @property int|null $good_num 商品数量
 * @property float|null $good_price 商品价格
 * @property float|null $total_price 商品总价
 * @property float|null $pay_price 支付金额
 * @property float|null $discount_amount 优惠金额
 * @property string|null $create_time 下单时间
 * @property string|null $update_time 修改时间
 * @property int|null $campus_id 校区id
 * @property float|null $trade_price 批发价
 * @property float|null $ca_price 校区价
 */
class OrderGoods extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%order_goods}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_id', 'good_id', 'spec_id', 'good_num','campus_id'], 'integer'],
            [['good_info', 'spec_info'], 'string'],
            [['good_price', 'total_price', 'pay_price', 'discount_amount','trade_price','ca_price'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['spec_name', 'good_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => '订单id',
            'good_id' => '商品id',
            'spec_id' => '规格id',
            'spec_name' => '规格名称',
            'good_name' => '商品名称',
            'good_info' => '商品信息',
            'spec_info' => '规格信息',
            'good_num' => '商品数量',
            'good_price' => '商品价格',
            'total_price' => '商品总价',
            'pay_price' => '支付金额',
            'discount_amount' => '优惠金额',
            'create_time' => '下单时间',
            'update_time' => '修改时间',
            'campus_id' => '校区id',
            'trade_price' => '批发价',
            'ca_price' => '校区价',
        ];
    }

    /**
     * 获取订单商品
     * @param $ids 订单id
     * @return array
     */
    public function getList($ids)
    {
        $data = self::find()->where(['order_id'=>$ids])->asArray()->all();
        $arr = [];
        foreach ($data as $datum){
            $good_info = json_decode($datum['good_info'],true);
            $one = [
                'main_img'=>commonApi::getDomain($good_info['main_img']),
                'good_name'=>$datum['good_name'],
                'spec_name'=>$datum['spec_name'],
                'good_num'=>$datum['good_num'],
                'good_price'=>$datum['good_price'],
                'total_price'=>$datum['total_price'],
                'ca_price'=>$datum['ca_price'],
                'trade_price'=>$datum['trade_price'],
            ];
            $arr[$datum['order_id']][] = $one;
        }
        return $arr;
    }

    public function getHtml($ids)
    {
        $data = $this->getList($ids);
        $html = [];
        foreach ($data as $k=>$datum){
            $str = "<div class='layui-table-cell laytable-cell-1-0-3' id='main_picture_{$k}'>";
            $str .= '<div class="goods-content">';
            foreach ($datum as $item){
                $str.= '<div style="text-align: left;" class="goods-data">';
                $str.= "<img src='{$item['main_img']}' style='height:80px;width: 80px' class='layui-col-md4'>";
                $str.= "<div class='layui-input-inline' style='margin-left: 10px'>";
                $str.= "<span class='layui-col-md7 goods_name_hide'>{$item['good_name']}</span>";
                $str.= "<span class='layui-col-md5'>￥{$item['good_price']}</span><br>";
                $str.= "<span class='layui-col-md7 goods_name_hide'>{$item['spec_name']}</span><span class='layui-col-md5'>{$item['good_num']}件</span>";
                $str.= '</div>';
                $str.= '</div>';
            }
            $str .= '</div>';
            $str .= '</div>';
            $html[$k] = $str;
        }
        return $html;
    }
}
